package com.xiyu.service.repository.fee.deposit;

import com.xiyu.service.model.elder.info.ElderInfoFetcher;
import com.xiyu.service.model.fee.deposit.FeeDepositFetcher;
import com.xiyu.service.vo.fee.feeDeposit.FeeDepositPageQueryInput;
import com.xiyu.service.model.fee.deposit.FeeDeposit;
import com.xiyu.service.model.fee.deposit.FeeDepositTable;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;
import org.springframework.util.StringUtils;
import java.util.Optional;

public interface FeeDepositRepository extends JRepository<FeeDeposit, Long> {
    FeeDepositTable feeDepositTable = FeeDepositTable.$;

    default Page<FeeDeposit> pageQuery(FeeDepositPageQueryInput inputVO){
        return  sql().createQuery(feeDepositTable)
                .whereIf(StringUtils.hasText(inputVO.getName()), () -> feeDepositTable.elder().name().like(inputVO.getName()))
                .whereIf(StringUtils.hasText(inputVO.getIdentityCard()), () -> feeDepositTable.elder().identityCard().like(inputVO.getIdentityCard()))
                .whereIf(inputVO.getCheckinDate() != null, () -> feeDepositTable.elder().checkinDate().between(inputVO.getCheckinDate()[0].toString(), inputVO.getCheckinDate()[1].toString()))
                .orderBy(feeDepositTable.id().desc())
                .select(feeDepositTable.fetch(FeeDepositFetcher.$.allTableFields().elder(ElderInfoFetcher.$.allTableFields())))
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());

    }

    Optional<FeeDeposit> findByElderId(Long elderId);

}